package com.book.filter;/**
 * Created by CoolWen on 2017/5/12.
 */

import com.book.entity.Student;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author CoolWen
 * @version 2017-05-12 9:10
 */
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest hsp = (HttpServletRequest) req;
        Student student = (Student) hsp.getSession().getAttribute("LoginUser");
        if (student == null) {
            ((HttpServletResponse) resp).sendRedirect(hsp.getContextPath() + "/login");
        }
        chain.doFilter(req, resp);
    }

    @Override
    public void destroy() {

    }
}
